package com.wedimob.slideshare.task;

import java.util.Observable;
import java.util.concurrent.ThreadPoolExecutor;

public class Monitor extends Observable implements Runnable {

    private ThreadPoolExecutor executor;
    private double progress;

    public Monitor(ThreadPoolExecutor executor) {
        super();
        this.executor = executor;
    }
    
    public void setProgress(double progress) {
        if(progress != this.progress) {
            setChanged();
        }
        this.progress = progress;
        notifyObservers(progress);
    }

    @Override
    public void run() {
        try {
            do {
                if (executor.getTaskCount() > 0
                        && executor.getCompletedTaskCount() == executor
                                .getTaskCount()) {
                    executor.shutdown();
                }

                setProgress( 1.0 * executor.getCompletedTaskCount() / executor.getTaskCount() );                
                Thread.sleep(2000);
            } while (true);
        } catch (Exception e) {
        }
    }

}
